
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * DialogNouvelleRegles.java
 *
 * Created on 2 mai 2011, 09:14:40
 */
package Vue;

import java.util.ArrayList;

/**
 * Classe permettant de créer une fenêtre de dialogue pour modifier les règles du jeu de la vie.
 * @author FERRIER/NONGLATON
 */
public class DialogNouvelleRegles extends javax.swing.JDialog {

    private boolean reussi;

    /** Créé une nouvelle fenêtre de dialogue pour la gestion de la règle
     * @param parent la fenêtre parent
     * @param modal true si la fenêtre doit être modale, false sinon
     * @param arrayNaiss liste des nombres de cellules voisines permettant la naissance
     * @param arraySurvie liste des nombres de cellules voisines permettant la survie
     */
    public DialogNouvelleRegles(java.awt.Frame parent, boolean modal, ArrayList<Integer> arrayNaiss, ArrayList<Integer> arraySurvie) {
        super(parent, modal);

        initComponents();

        this.setLocationRelativeTo(this.getParent());

        setNaissance(arrayNaiss);
        setSurvie(arraySurvie);

        reussi = false;
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        panelNaissance = new javax.swing.JPanel();
        chkBoxNaiss1 = new javax.swing.JCheckBox();
        chkBoxNaiss2 = new javax.swing.JCheckBox();
        chkBoxNaiss8 = new javax.swing.JCheckBox();
        chkBoxNaiss7 = new javax.swing.JCheckBox();
        chkBoxNaiss5 = new javax.swing.JCheckBox();
        chkBoxNaiss6 = new javax.swing.JCheckBox();
        chkBoxNaiss4 = new javax.swing.JCheckBox();
        chkBoxNaiss3 = new javax.swing.JCheckBox();
        panelSurvie = new javax.swing.JPanel();
        chkBoxSurvie2 = new javax.swing.JCheckBox();
        chkBoxSurvie6 = new javax.swing.JCheckBox();
        chkBoxSurvie8 = new javax.swing.JCheckBox();
        chkBoxSurvie5 = new javax.swing.JCheckBox();
        chkBoxSurvie7 = new javax.swing.JCheckBox();
        chkBoxSurvie3 = new javax.swing.JCheckBox();
        chkBoxSurvie1 = new javax.swing.JCheckBox();
        chkBoxSurvie4 = new javax.swing.JCheckBox();
        boutonOK = new javax.swing.JButton();
        boutonAnnuler = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("RÃ¨gles");
        setResizable(false);

        panelNaissance.setBorder(javax.swing.BorderFactory.createTitledBorder("Naissance"));
        panelNaissance.setToolTipText("Choisir les rÃ¨gles de naissance.");

        chkBoxNaiss1.setText("1");

        chkBoxNaiss2.setText("2");

        chkBoxNaiss8.setText("8");

        chkBoxNaiss7.setText("7");

        chkBoxNaiss5.setText("5");

        chkBoxNaiss6.setText("6");

        chkBoxNaiss4.setText("4");

        chkBoxNaiss3.setText("3");

        javax.swing.GroupLayout panelNaissanceLayout = new javax.swing.GroupLayout(panelNaissance);
        panelNaissance.setLayout(panelNaissanceLayout);
        panelNaissanceLayout.setHorizontalGroup(
            panelNaissanceLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(panelNaissanceLayout.createSequentialGroup()
                .addGap(17, 17, 17)
                .addGroup(panelNaissanceLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER)
                    .addComponent(chkBoxNaiss1)
                    .addComponent(chkBoxNaiss8)
                    .addComponent(chkBoxNaiss7)
                    .addComponent(chkBoxNaiss5)
                    .addComponent(chkBoxNaiss2)
                    .addComponent(chkBoxNaiss6)
                    .addComponent(chkBoxNaiss4)
                    .addComponent(chkBoxNaiss3))
                .addContainerGap(17, Short.MAX_VALUE))
        );
        panelNaissanceLayout.setVerticalGroup(
            panelNaissanceLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(panelNaissanceLayout.createSequentialGroup()
                .addComponent(chkBoxNaiss1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(chkBoxNaiss2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(chkBoxNaiss3)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(chkBoxNaiss4)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(chkBoxNaiss5)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(chkBoxNaiss6)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(chkBoxNaiss7)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(chkBoxNaiss8)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        panelSurvie.setBorder(javax.swing.BorderFactory.createTitledBorder("Survie"));
        panelSurvie.setToolTipText("Choisir les rÃ¨gles de survie.");

        chkBoxSurvie2.setText("2");

        chkBoxSurvie6.setText("6");

        chkBoxSurvie8.setText("8");

        chkBoxSurvie5.setText("5");

        chkBoxSurvie7.setText("7");

        chkBoxSurvie3.setText("3");

        chkBoxSurvie1.setText("1");

        chkBoxSurvie4.setText("4");

        javax.swing.GroupLayout panelSurvieLayout = new javax.swing.GroupLayout(panelSurvie);
        panelSurvie.setLayout(panelSurvieLayout);
        panelSurvieLayout.setHorizontalGroup(
            panelSurvieLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, panelSurvieLayout.createSequentialGroup()
                .addContainerGap(18, Short.MAX_VALUE)
                .addGroup(panelSurvieLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(chkBoxSurvie8)
                    .addComponent(chkBoxSurvie7)
                    .addComponent(chkBoxSurvie5)
                    .addComponent(chkBoxSurvie6)
                    .addComponent(chkBoxSurvie4)
                    .addComponent(chkBoxSurvie3)
                    .addComponent(chkBoxSurvie1)
                    .addComponent(chkBoxSurvie2))
                .addGap(16, 16, 16))
        );
        panelSurvieLayout.setVerticalGroup(
            panelSurvieLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(panelSurvieLayout.createSequentialGroup()
                .addComponent(chkBoxSurvie1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(chkBoxSurvie2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(chkBoxSurvie3)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(chkBoxSurvie4)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(chkBoxSurvie5)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(chkBoxSurvie6)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(chkBoxSurvie7)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(chkBoxSurvie8)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        boutonOK.setText("OK");
        boutonOK.setMaximumSize(new java.awt.Dimension(75, 23));
        boutonOK.setMinimumSize(new java.awt.Dimension(75, 23));
        boutonOK.setPreferredSize(new java.awt.Dimension(75, 23));
        boutonOK.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                boutonOKActionPerformed(evt);
            }
        });

        boutonAnnuler.setText("Annuler");
        boutonAnnuler.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                boutonAnnulerActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(boutonOK, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 10, Short.MAX_VALUE)
                        .addComponent(boutonAnnuler))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(panelNaissance, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(panelSurvie, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );

        layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {panelNaissance, panelSurvie});

        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(panelNaissance, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(panelSurvie, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(boutonOK, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(boutonAnnuler))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void boutonOKActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_boutonOKActionPerformed
        reussi = true;
        this.dispose();
    }//GEN-LAST:event_boutonOKActionPerformed

    private void boutonAnnulerActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_boutonAnnulerActionPerformed
        this.dispose();
    }//GEN-LAST:event_boutonAnnulerActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton boutonAnnuler;
    private javax.swing.JButton boutonOK;
    private javax.swing.JCheckBox chkBoxNaiss1;
    private javax.swing.JCheckBox chkBoxNaiss2;
    private javax.swing.JCheckBox chkBoxNaiss3;
    private javax.swing.JCheckBox chkBoxNaiss4;
    private javax.swing.JCheckBox chkBoxNaiss5;
    private javax.swing.JCheckBox chkBoxNaiss6;
    private javax.swing.JCheckBox chkBoxNaiss7;
    private javax.swing.JCheckBox chkBoxNaiss8;
    private javax.swing.JCheckBox chkBoxSurvie1;
    private javax.swing.JCheckBox chkBoxSurvie2;
    private javax.swing.JCheckBox chkBoxSurvie3;
    private javax.swing.JCheckBox chkBoxSurvie4;
    private javax.swing.JCheckBox chkBoxSurvie5;
    private javax.swing.JCheckBox chkBoxSurvie6;
    private javax.swing.JCheckBox chkBoxSurvie7;
    private javax.swing.JCheckBox chkBoxSurvie8;
    private javax.swing.JPanel panelNaissance;
    private javax.swing.JPanel panelSurvie;
    // End of variables declaration//GEN-END:variables

    /**
     * Permet de cocher les cases correspondant à la règle actuelle pour la naissance
     * @param naiss liste des nombres de cellules voisines permettant la naissance
     */
    private void setNaissance(ArrayList<Integer> naiss) {

        for (Integer i : naiss) {
            switch (i) {
                case 1:
                    chkBoxNaiss1.setSelected(true);
                    break;
                case 2:
                    chkBoxNaiss2.setSelected(true);
                    break;
                case 3:
                    chkBoxNaiss3.setSelected(true);
                    break;
                case 4:
                    chkBoxNaiss4.setSelected(true);
                    break;
                case 5:
                    chkBoxNaiss5.setSelected(true);
                    break;
                case 6:
                    chkBoxNaiss6.setSelected(true);
                    break;
                case 7:
                    chkBoxNaiss7.setSelected(true);
                    break;
                case 8:
                    chkBoxNaiss8.setSelected(true);
                    break;
            }
        }
    }

    
    /**
     * Permet de cocher les cases correspondant à la règle actuelle pour la survie
     * @param naiss liste des nombres de cellules voisines permettant la survie
     */
    private void setSurvie(ArrayList<Integer> Survie) {

        for (Integer i : Survie) {
            switch (i) {
                case 1:
                    chkBoxSurvie1.setSelected(true);
                    break;
                case 2:
                    chkBoxSurvie2.setSelected(true);
                    break;
                case 3:
                    chkBoxSurvie3.setSelected(true);
                    break;
                case 4:
                    chkBoxSurvie4.setSelected(true);
                    break;
                case 5:
                    chkBoxSurvie5.setSelected(true);
                    break;
                case 6:
                    chkBoxSurvie6.setSelected(true);
                    break;
                case 7:
                    chkBoxSurvie7.setSelected(true);
                    break;
                case 8:
                    chkBoxSurvie8.setSelected(true);
                    break;
            }
        }
    }

    
    /**
     * Retourne la liste correspondant aux cases cochées pour la naissance
     * @return liste des nombres de cellules voisines permettant la naissance
     */
    public ArrayList<Integer> getNaissance() {
        ArrayList<Integer> arrayNaiss = new ArrayList<Integer>(0);
        if (chkBoxNaiss1.isSelected()) {
            arrayNaiss.add(1);
        }
        if (chkBoxNaiss2.isSelected()) {
            arrayNaiss.add(2);
        }
        if (chkBoxNaiss3.isSelected()) {
            arrayNaiss.add(3);
        }
        if (chkBoxNaiss4.isSelected()) {
            arrayNaiss.add(4);
        }
        if (chkBoxNaiss5.isSelected()) {
            arrayNaiss.add(5);
        }
        if (chkBoxNaiss6.isSelected()) {
            arrayNaiss.add(6);
        }
        if (chkBoxNaiss7.isSelected()) {
            arrayNaiss.add(7);
        }
        if (chkBoxNaiss8.isSelected()) {
            arrayNaiss.add(8);
        }

        return arrayNaiss;
    }

    
    /**
     * Retourne la liste correspondant aux cases cochées pour la survie
     * @return liste des nombres de cellules voisines permettant la survie
     */
    public ArrayList<Integer> getSurvie() {

        ArrayList<Integer> arraySurvie = new ArrayList<Integer>(0);

        if (chkBoxSurvie1.isSelected()) {
            arraySurvie.add(1);
        }
        if (chkBoxSurvie2.isSelected()) {
            arraySurvie.add(2);
        }
        if (chkBoxSurvie3.isSelected()) {
            arraySurvie.add(3);
        }
        if (chkBoxSurvie4.isSelected()) {
            arraySurvie.add(4);
        }
        if (chkBoxSurvie5.isSelected()) {
            arraySurvie.add(5);
        }
        if (chkBoxSurvie6.isSelected()) {
            arraySurvie.add(6);
        }
        if (chkBoxSurvie7.isSelected()) {
            arraySurvie.add(7);
        }
        if (chkBoxSurvie8.isSelected()) {
            arraySurvie.add(8);
        }

        return arraySurvie;
    }

    /**
     * Retourne si l'utilisateur a validé ou annulé
     * @return true si l'utilisateur a validé et false si il a annulé
     */
    public boolean isReussi() {
        return reussi;
    }
}